<?php
namespace Home\Controller;

class BikeController extends CommonController{

	public function index(){
		$where = array();
		$where['user_id'] = $this->user['id'];
		$where['status'] = array('neq','1');
		$list = D('Bike')->where($where)->relation(true)->select();
        $this->assign('list',$list);
		$this->display();
	}

	public function bike_detail(){
		$id = (int)I('get.id');
		$bike_info = D('Bike')->relation(true)->where(array('id'=>$id))->find();
		$bike_info || $this->error_msg('抱歉，没有对应的车辆信息',U('Bike/index'));
		$bike_info['goods_detail'] = D('Goods')->where(array('id'=>$bike_info['goods_id']))->getField('detail');
		$this->assign('bike_info',$bike_info);
		$this->display();
	}	
	
	public function turn_info(){
		if(IS_POST){
			$data = I('post.info');
			if(!D('TurnBike')->create($data))$this->error(D('TurnBike')->getError());
			$result = D('TurnBike')->saveData($data);
			if ($result) {
				//将车辆的状态更新为’出让中‘
				D('Bike')->where(array('id'=>$data['bike_id']))->save(array('status'=>'3'));
				$this->success('出让信息提交次成功！',U('Bike/index'));
			}else{
				$this->error('系统繁忙，提交失败！');
			}
		}else{
			$bike_id = (int)I('get.bike_id');
			$bike_info = D('Bike')->relation(true)->where(array('id'=>$bike_id))->find();
			$bike_info || $this->error_msg('抱歉，没有对应的车辆信息',U('Bike/index'));
			($bike_info['service'] != '3') && $this->error_msg('抱歉，该车未选择按天付服务，不可转让',U('Bike/index'));
			$repayment = D('Repayment')->where(array('order_id'=>$bike_info['order_id']))->find();
			$this->assign('bike_info',$bike_info);
			$this->assign('repayment',$repayment);
			$this->display();
		}
	}
	
	public function info(){
		
		$this->display();
	}	
	
}